// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.neuronbit.businessflow.intellij.editor.swing.editor;

//import com.intellij.uiDesigner.componentTree.String;
//import com.intellij.uiDesigner.componentTree.ComponentSelectionListener;
//import com.intellij.uiDesigner.designSurface.GuiEditor;
import com.intellij.util.concurrency.ThreadingAssertions;
import com.intellij.util.containers.Stack;
import com.mxgraph.model.mxCell;
import com.mxgraph.model.mxICell;
import com.neuronbit.businessflow.intellij.editor.swing.GuiEditor;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Collections;

/**
 * This class implements Ctrl+W / Ctrl+Shift+W functionality in the GuiEditor
 */
public final class SelectionState {
  private final Stack<String[]> mySelectionHistory;
  /** We do not need to handle our own events */
  private boolean myInsideChange;

    /**
     * Instantiates a new Selection state.
     *
     * @param editor the editor
     */
    public SelectionState(@NotNull final GuiEditor editor) {
    mySelectionHistory = new Stack<>();
    //editor.addComponentSelectionListener(new MyComponentSelectionListener());
  }

    /**
     * Set inside change.
     *
     * @param insideChange the inside change
     */
    public void setInsideChange(final boolean insideChange){
    ThreadingAssertions.assertEventDispatchThread();
    myInsideChange = insideChange;
  }

    /**
     * Gets selection history.
     *
     * @return the selection history
     */
    public Stack<String[]> getSelectionHistory() {
    return mySelectionHistory;
  }

    /**
     * Get selection string [ ].
     *
     * @param editor the editor
     * @return the string [ ]
     */
    public static String[] getSelection(final GuiEditor editor){
    final  RadComponent[] selection = editor.getGraphComponent().getSelectedComponents();
    final String[] ptrs = new String[selection.length];
    for(int i = selection.length - 1; i >= 0; i--){
      ptrs[i] = getPath(selection[i].getDelegee());
    }
    return ptrs;
  }

    /**
     * Gets path.
     *
     * @param cell the cell
     * @return the path
     */
    public static String getPath(mxICell cell) {
    Stack<String> path = new Stack<>();
    path.push(cell.getId());
    mxICell x = cell;
    while (x.getParent()!=null) {
      path.push(x.getParent().getId());
      x = x.getParent();
    }
    Collections.reverse(path);
    return String.join("/", path);
  }

    /**
     * Restore selection.
     *
     * @param editor the editor
     * @param ptrs   the ptrs
     */
    public static void restoreSelection(final GuiEditor editor, final String[] ptrs) {
    editor.getGraphComponent().clearSelection();
    editor.getGraphComponent().setSelection(ptrs);
  }

//  private final class MyComponentSelectionListener implements ComponentSelectionListener{
//
//    @Override
//    public void selectedComponentChanged(@NotNull final GuiEditor source) {
//      if(myInsideChange){ // do not react on own events
//        return;
//      }
//      mySelectionHistory.clear();
//      mySelectionHistory.push(getSelection(source));
//    }
//  }

}
